package com.zjh.workflow.flowable.service.impl;

import com.zjh.admin.entity.DAO.SysUserEntity;
import com.zjh.admin.service.UserService;
import com.zjh.workflow.flowable.cmd.AddHisCommentCmd;
import com.zjh.workflow.flowable.entity.enums.CommentTypeEnum;
import com.zjh.workflow.flowable.entity.vo.CommentVo;
import com.zjh.workflow.flowable.mapper.IFlowableCommentMapper;
import com.zjh.workflow.flowable.service.IFlowableCommentService;
import org.flowable.engine.ManagementService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author : bruce.liu
 * @title: : FlowCommentServiceImpl
 * @projectName : flowable
 * @description: 流程备注service
 * @date : 2019/11/2412:58
 */
@Service
public class FlowableCommentServiceImpl implements IFlowableCommentService {

    @Resource
    protected ManagementService managementService;

    @Resource
    private IFlowableCommentMapper flowableCommentMapper;

    @Resource
    private UserService userService;

    @Override
    public void addComment(CommentVo comment) {
        managementService.executeCommand(new AddHisCommentCmd(comment.getTaskId(), comment.getUserId(), comment.getProcessInstanceId(),
                comment.getType(), comment.getMessage()));
    }

    @Override
    public List<CommentVo> getFlowCommentVosByProcessInstanceId(String processInstanceId) {
        List<CommentVo> commentVos = flowableCommentMapper.getFlowCommentVosByProcessInstanceId(processInstanceId);
        commentVos.forEach(commentVo -> {
            String userId = commentVo.getUserId();
            SysUserEntity userById = userService.getUserById(Long.valueOf(userId));
            commentVo.setUserName(userById.getUsername());
            commentVo.setUserNickName(userById.getNickname());
            commentVo.setTypeName(CommentTypeEnum.getEnumMsgByType(commentVo.getType()));
        });
        return commentVos;
    }
}
